<?php

namespace app\api\logic;
use app\admin\logic\BasicLogic;
use think\facade\Db;
class DulizhanLogic extends BasicLogic
{
    public function AddSKUDetails($params=[]){
        $id       = $params['id']??'';
        if(!$id)   return ['msg'=>'订单ID不能为空!'];

        $test_order_quantity = Db::connect('DULIZHAN')
            ->name('order_quantity')
            ->where('order_id',$id)
            ->select();
        $skuinfo = "";
        $skuinfo2 = "";
        $new_spu = [];
        if(!empty($test_order_quantity)){
            foreach ($test_order_quantity as $kq=>$vq) {
                $insert = [];
                $insert['order_id'] = $vq['order_id'];
                $insert['sku'] = $vq['sku'];
                $productsku = $this->productsku($vq);
                $insert['num'] = $productsku['pieces_new'];;
                $insert['spu'] = $productsku['brand'];
                $new_spu[$productsku['brand']] = 1;
                if(!empty($skuinfo)){
                    $skuinfo = $skuinfo.",".$vq['sku']. "*" .$vq['quantity'];
                }else{
                    $skuinfo = $vq['sku']. "*" .$vq['quantity'];
                }
                if(!empty($skuinfo2)){
                    $skuinfo2 = $skuinfo2.",".$insert['spu']. "*" .$insert['num'];
                }else{
                    $skuinfo2 = $insert['spu']. "*" .$insert['num'];
                }
            }
        }
        
        $update_info = [];
        $update_info['skuinfo'] = $skuinfo;
        $update_info['skuinfo2'] = $skuinfo2;
        $update_info['skuinfo_time'] = time();

        if(!empty($new_spu)){
            $update_info['two_kf_id'] = count($new_spu);
        }else{
            $update_info['two_kf_id'] = 1;
        }
        
        if(!empty($update_info)){
            Db::connect('DULIZHAN')
            ->name('order')
            ->where("id",$id)
            ->update($update_info);
        }
        
        return  true;
    }


    /**
    * 方法详情
    * @param array params
    */
    public function productsku($v)
    {
        $productsku['brand'] = "";
        $productsku['pieces_new'] = 0;
        $count = 0;
        if (!empty($v['sku']) && !empty($v['quantity'])) {
            //拆分规则
            $pos1 = strpos($v['sku'], '+');
            if ($pos1 !== false) {
                $pos = strpos($v['sku'], '-');
                if ($pos !== false) {
                    $sku = explode('-', $v['sku']);
                    $productsku['brand'] = $sku[0];
                    $combination_sku = explode('+', $sku[1]);
                    $count = count($combination_sku);
                    if(isset($sku[3]) && !empty($sku[3])){
                        $count = (int)number_format((int)$sku[3] * (int)$count, 0, '.', '');
                    }
                }else{
                    $count = 2;
                }
            } else {
                $pos = strpos($v['sku'], '-');
                if ($pos !== false) {
                    $sku = explode('-', $v['sku']);
                    $productsku['brand'] = $sku[0];
                    if (isset($sku[3])) {
                        if (is_numeric($sku[3])) {
                            $count = $sku[3];
                        }
                    } else {
                        $count = 1;
                    }
                }else{
                    $count = 2;
                }
            }
            $count = (int)number_format((int)$v['quantity'] * (int)$count, 0, '.', '');
        }
        $productsku['pieces_new'] = $count;
        return $productsku; 
    }

    //整理订单是否被结算
    public function HasTheOrderBeenSettled($params=[]){
        $id       = $params['order_id']??'';
        if(!$id)   return ['msg'=>'订单ID不能为空!'];

        $test_order_quantity = Db::connect('DULIZHAN')
            ->name('jiesuaninfo')
            ->where('order_id',$id)
            ->select();

        $update_info = [];
        if(!empty($test_order_quantity)){
            foreach ($test_order_quantity as $kq=>$vq) {
                $key = "info".$kq;
                $update_info[$key] = $vq['money'];
            }
            $update_info["status"] = "完成";
        }else{
            $update_info["status"] = "未找到";
        }
        
        if(!empty($update_info)){
            Db::connect('DULIZHAN')
                ->name('jiesuan')
                ->where("order_id",$id)
                ->update($update_info);
        }
        
        return  true;
    }
    //整理订单是否匹配到运营跟渠道
    public function MatchingToOperationsAndChannels($v){
        $customerjourneys = $this->customerjourney($v);
        $tag_source = $customerjourneys['tag_source'];
        $tag_campaign = $customerjourneys['tag_campaign'];
        $msg_data = array();
        $update = array();
        if(!empty($tag_source)){
            $update["tag_source"] = strtolower($tag_source);
            $msg_data[] = strtolower($tag_source);
        }else{
            $update["tag_source"] = "";
        }
        if(!empty($tag_campaign)){
            $update["tag_campaign"] = strtolower($tag_campaign);
            $msg_data[] = strtolower($tag_campaign);
        }else{
            $update["tag_campaign"] = "";
        }
        if(!empty($msg_data)){
            $update['status'] = 1;
            if($update["tag_source"] == "other"){
                $update['status'] = 2;
            }
        }else{
            $update['status'] = 2;
        }
        
        Db::connect('DULIZHAN')
            ->name('customerjourney')
            ->where("id",$v['id'])
            ->update($update);
        
        return  true;
    }


    /**
    * 方法详情
    * @param array params
    */
    public function customerjourney($customerjourney)
    {
        $str = $this->config();
        $tag_source = "";
        $tag_campaign = "";
        //重写规则
            $summary = Db::connect('DULIZHAN')
                ->name('order')
                ->field('b.lastVisit_campaign,b.lastVisit_source,b.firstVisit_source,b.firstVisit_campaign,a.landing_site')
                ->alias('a')
                ->join('qu_customerjourneysummary b','a.id = b.order_id', 'left')
                ->where('a.order_number',$customerjourney['order_number'])
                ->where('a.site_id',$customerjourney['site_id'])
                ->find();
            //新规则 6月6
            if(!empty($summary['lastVisit_campaign']) && !empty($summary['landing_site'])){
                $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                if(!empty($tag_campaign)){
                    $pos = strpos($summary['landing_site'], $tag_campaign);
                    if ($pos !== false) {
                        $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                        $customerjourneys['tag_campaign'] = $tag_campaign;
                        return $customerjourneys;
                    }
                }
            }
            if(!empty($summary['lastVisit_campaign'])){
                $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                if(!empty($tag_campaign)){
                    $customerjourneys['tag_source'] = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                    $customerjourneys['tag_campaign'] = $tag_campaign;
                    return $customerjourneys;
                }
            }
            //情况3 只有landing_site有数据
            if(!empty($summary['landing_site'])){
                $tag_campaign = $this->tag_landing_site($summary['landing_site'],$str);
                if(!empty($tag_campaign)){
                    $customerjourneys['tag_source'] = $this->tag_campaign_tag($tag_campaign,$str);
                    $customerjourneys['tag_campaign'] = $tag_campaign;
                    return $customerjourneys;
                }
            }
            if(!empty($summary['landing_site'])){
                $tag_source = $this->tag_site($summary['landing_site'],$str);
                if(!empty($tag_source)){
                    $customerjourneys['tag_source'] = $tag_source;
                    //0614补充
                    $customerjourneys['tag_campaign'] = "";
                    if(!empty($summary['lastVisit_campaign']) && empty($customerjourneys['tag_campaign'])){
                        $tag_campaign = $this->tag_campaign($summary['lastVisit_campaign'],$str);
                        if(!empty($tag_campaign)){
                            $customerjourneys_tag_source = $this->tag_campaign_tag($summary['lastVisit_campaign'],$str);
                            if($customerjourneys['tag_source'] == $customerjourneys_tag_source){
                                $customerjourneys['tag_campaign'] = $tag_campaign;
                            }
                        }
                    }
                    if(!empty($customerjourney['last_campaign']) && empty($customerjourneys['tag_campaign'])){
                        $tag_campaign = $this->tag_campaign($customerjourney['last_campaign'],$str);
                        if(!empty($tag_campaign)){
                            $customerjourneys_tag_source = $this->tag_campaign_tag($customerjourney['last_campaign'],$str);
                            if($customerjourneys['tag_source'] == $customerjourneys_tag_source){
                                $customerjourneys['tag_campaign'] = $tag_campaign;
                            }
                        }
                    }
                    return $customerjourneys;
                }
            }
            if(!empty($summary['landing_site'])){
                $domain = stristr($summary['landing_site'],"bra60-");
                if($domain){
                    $customerjourneys['tag_source'] = "fb";
                    $customerjourneys['tag_campaign'] = "ZX";
                    return $customerjourneys;
                }
            }
            if($customerjourney['first_source'] == "direct" || $customerjourney['first_campaign'] == "www.google.com/organic"){
                $customerjourneys['tag_source'] = "直接访问";
                $customerjourneys['tag_campaign'] = "";
                return $customerjourneys;
            }
            if($customerjourney['last_campaign'] == "linktr.ee/referral" || $customerjourney['last_source'] == "linktr.ee"){
                $customerjourneys['tag_source'] = "fb";
                $customerjourneys['tag_campaign'] = "";
                return $customerjourneys;
            }
            if($customerjourney['last_campaign'] == "--/ads"){
                $customerjourneys['tag_source'] = "tt";
                $customerjourneys['tag_campaign'] = "";
                return $customerjourneys;
            }
            
            $customerjourneys['tag_source'] = "";
            $customerjourneys['tag_campaign'] = "";
        return $customerjourneys;
    }

    /**
    * 方法详情
    * @param array params
    */
    public function config()
    {
        $data['str1'] = [
            "Facebook",
            "Instagram",
            "TikTok",
            // "omnisend",
            "ad_google",
        ];
        $data['str2'] = [
            "fb",
            "fb",
            "tt",
            // "email",
            "ggad",
        ];
        $data['str3'] = [
            "lxf",
            "ZX",
            "RJW",
            "TXL",
            "MF",
            "khb",
            "HJ",
            "hsw",
            "bwh",
            "zby",
            "wwj",
            "xlh",
            "zb",
            "gjw",
        ];
        $data['str4'] = [
            "李晓飞",
            "张兴",
            "任洁文",
            "陶小兰",
            "马芳",
            "柯海波",
            "胡锦",
            "侯思维",
            "白文华",
            "章博语",
            "伍文娟",
            "许良海",
            "曾贝",
            "郭佳伟",
        ];
        $data['str5'] = [
            "64",
            "10",
            "16",
            "49",
            "76",
            "9",
            "13",
            "78",
            "47",
            "66",
            "8",
            "77",
            "12",
            "82",
        ];
        $data['str6'] = [
            "Direct",
            "google.com",
            "organic",
        ];
        $data['str7'] = [
            "fb",
            "fb",
            "fb",
            "fb",
            "ggad",
            "tt",
            "tt",
            "tt",
            "tt",
            "ggad",
            "fb",
            "tt",
            "tt",
            "fb",
        ];
        $data['str8'] = [
            "fb",
            "tt",
            "ggad",
            "直接访问",
            "email",
            "other",
            "空值",
            "impact",
        ];
        $data['str9'] = [
            "lxf",
            "ZX",
            "RJW",
            "TXL",
            "MF",
            "khb",
            "HJ",
            "hsw",
            "bwh",
            "zby",
            "wwj",
            "xlh",
            "zb",
            "gjw",
            "空值",
        ];
        $data['str10'] = [
            "fbclid",
            "gclid",
            "ttclid",
        ];
        $data['str11'] = [
            "fb",
            "ggad",
            "tt",
        ];
        return $data;
    }

    /**
    * 方法详情
    * @param array params
    */
    public function tag_campaign($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str3'] as $k=>$v) {
                $domain = stristr($source,$v);
                if($domain){
                    if($v == "bra60"){
                        $msg = "ZX";
                    }elseif($v == "fei"){
                        $msg = "lxf";
                    }else{
                        $msg = $v;
                    }
                    break;
                }
            }
        }
        return $msg;
    }
	
	/**
    * 方法详情
    * @param array params
    */
    public function tag_campaign_tag($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str3'] as $k=>$v) {
                $domain = stristr($source,$v);
                if($domain){
                    $msg = $str['str7'][$k];
                    break;
                }
            }
        }
        return $msg;
    }
	
	/**
    * 方法详情
    * @param array params
    */
    public function tag_landing_site($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str3'] as $k=>$v) {
                $domain = stristr($source,$v."-");
                if($domain){
                    $msg = $str['str3'][$k];
                    break;
                }
            }
        }
        return $msg;
    }
	
	/**
    * 方法详情
    * @param array params
    */
    public function tag_site($source,$str)
    {
        $msg = "";
        if(!empty($source)){
            foreach ($str['str10'] as $k=>$v) {
                $domain = stristr($source,$v);
                if($domain){
                    $msg = $str['str11'][$k];
                    break;
                }
            }
        }
        return $msg;
    }

}